package com.yuliang.ai.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuliang.ai.domain.Dto.HumanPicInfoDto;
import com.yuliang.ai.domain.Dto.UserChatInfoDto;
import com.yuliang.ai.domain.Vo.HumanPicInfoVo;
import com.yuliang.ai.domain.Vo.UserChatInfoVo;
import com.yuliang.ai.domain.entity.UserChatInfo;
import com.yuliang.ai.mapper.UserChatInfoMapper;
import com.yuliang.ai.service.HumanPicInfoService;
import com.yuliang.ai.service.UserChatInfoService;
import com.yuliang.utils.result.ResultUtils;
import com.yuliang.utils.result.emums.HttpCodeEnum;
import com.yuliang.utils.superbackage.SuperServiceImpl;
import com.yuliang.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class UserChatInfoServiceImpl extends SuperServiceImpl<UserChatInfoMapper, UserChatInfo> implements UserChatInfoService {

    @Autowired
    UserChatInfoService userChatInfoService;

    @Autowired
    HumanPicInfoService humanPicInfoService;

    @Override
    public List<UserChatInfoDto> getUserChatInfoList() {
        List<UserChatInfo> userChatInfoList = userChatInfoService.list();
        if(userChatInfoList != null && userChatInfoList.size() > 0){
            String jsonArrayStr = JSONArray.toJSONString(userChatInfoList);
            List<UserChatInfoDto>  userChatInfoDtoList = JSONArray.parseArray(jsonArrayStr, UserChatInfoDto.class);
            for(UserChatInfoDto userChatInfoDto : userChatInfoDtoList){
                HumanPicInfoVo humanPicInfoVo = new HumanPicInfoVo();
                humanPicInfoVo.setHumanIdentifyId(userChatInfoDto.getHumanIdentifyId());
                List<HumanPicInfoDto> humanPicInfoDtoList = humanPicInfoService.selectHumanPicInfoList(humanPicInfoVo);
                Map<Integer, List<HumanPicInfoDto>> humanPicInfoDtoMap = Optional.ofNullable(humanPicInfoDtoList).orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(HumanPicInfoDto::getType));
                List<UserChatInfoDto.HumanPicInfoDtoInfo> humanPicInfoDtoInfoList = new ArrayList<>();
                for(Map.Entry<Integer, List<HumanPicInfoDto>> entry : humanPicInfoDtoMap.entrySet()){
                    UserChatInfoDto.HumanPicInfoDtoInfo humanPicInfoDtoInfo = new UserChatInfoDto.HumanPicInfoDtoInfo();
                    humanPicInfoDtoInfo.setType(entry.getKey());
                    humanPicInfoDtoInfo.setHumanPicInfoDtoList(entry.getValue());
                    humanPicInfoDtoInfoList.add(humanPicInfoDtoInfo);
                }
                userChatInfoDto.setHumanPicInfoDtoInfoList(humanPicInfoDtoInfoList);
            }
            return userChatInfoDtoList;
        }
        return null;
    }

    @Override
    public List<UserChatInfoDto> getUserChatInfoDto(List<String> humanIdentifyIdList) {
        LambdaQueryWrapper<UserChatInfo> wrapper = new QueryWrapper<UserChatInfo>().lambda();
        wrapper.in(UserChatInfo::getHumanIdentifyId, humanIdentifyIdList);
        List<UserChatInfo> userChatInfoList = userChatInfoService.list(wrapper);
        if(userChatInfoList != null){
            String jsonArrayStr = JSONArray.toJSONString(userChatInfoList);
            List<UserChatInfoDto>  userChatInfoDtoList = JSONArray.parseArray(jsonArrayStr, UserChatInfoDto.class);
            for(UserChatInfoDto userChatInfoDto : userChatInfoDtoList){
                HumanPicInfoVo humanPicInfoVo = new HumanPicInfoVo();
                humanPicInfoVo.setHumanIdentifyId(userChatInfoDto.getHumanIdentifyId());
                List<HumanPicInfoDto> humanPicInfoDtoList = humanPicInfoService.selectHumanPicInfoList(humanPicInfoVo);
                Map<Integer, List<HumanPicInfoDto>> humanPicInfoDtoMap = Optional.ofNullable(humanPicInfoDtoList).orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(HumanPicInfoDto::getType));
                List<UserChatInfoDto.HumanPicInfoDtoInfo> humanPicInfoDtoInfoList = new ArrayList<>();
                for(Map.Entry<Integer, List<HumanPicInfoDto>> entry : humanPicInfoDtoMap.entrySet()){
                    UserChatInfoDto.HumanPicInfoDtoInfo humanPicInfoDtoInfo = new UserChatInfoDto.HumanPicInfoDtoInfo();
                    humanPicInfoDtoInfo.setType(entry.getKey());
                    humanPicInfoDtoInfo.setHumanPicInfoDtoList(entry.getValue());
                    humanPicInfoDtoInfoList.add(humanPicInfoDtoInfo);
                }
                userChatInfoDto.setHumanPicInfoDtoInfoList(humanPicInfoDtoInfoList);
            }
            return userChatInfoDtoList;
        }
        return null;
    }

    @Override
    public String insertUserChatInfo(UserChatInfoVo userChatInfoVo) {
        UserChatInfo userChatInfo = new UserChatInfo();
        userChatInfo.setUuid(StringUtils.getSnowFlakeId());
        userChatInfo.setHumanIdentifyId(StringUtils.getUUID());
        userChatInfo.setAge(userChatInfoVo.getAge());
        userChatInfo.setName(userChatInfoVo.getName());
        userChatInfo.setHumanTag(userChatInfoVo.getHumanTag());
        userChatInfo.setIntroduce(userChatInfoVo.getIntroduce());
        userChatInfo.insert();
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.insert_success);
    }
}
